package com.jhj.common.http;

import java.util.Map;

/**
 * 发送http请求并获取响应
 * 
 * @author GaoXiang Date: 2014-9-3
 */
public class JhjHttpSender {
    
    private static JhjHttpSender instance = new JhjHttpSender();
    
    private JhjHttpSender() {
    }
    
    public static JhjHttpSender getInstance() {
        return instance;
    }
    
    /**
     * 发送Get请求
     * 
     * @param url
     * @return
     * @throws Exception
     */
    public String sendGetRequest(String url) throws Exception {
        if (url.toLowerCase().startsWith("http://")) {
            return JhjSender.getInstance().sendHttpRequest(url);
        } else if (url.toLowerCase().startsWith("https://")) {
            return JhjHttpsSender.getInstance().sendHttpsGetRequest(url);
        }
        
        return null;
    }
    
    /**
     * 利用URL发起POST请求，并接收返回信息
     * 
     * @param url 请求URL
     * @param params 请求参数
     * @return 响应内容
     * @throws Exception
     */
    public String sendPostRequest(String url, Map<String, String> params) throws Exception {
        if (url.toLowerCase().startsWith("http://")) {
            return JhjSender.getInstance().sendPostRequest(url, params);
        } else if (url.toLowerCase().startsWith("https://")) {
            return JhjHttpsSender.getInstance().sendPostRequest(url, params);
        }
        
        return null;
    }
}
